<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :cols="2" :labelWidth="110" :formType="type" :isSubmitBtn="true" @formChange="formChangeHandle" />
  </div>
</template>
<script>
import { tableEdit } from '@/api/part/access/accessInventory';
export default {
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      formList: this.createColumns()
    };
  },
  mounted() {
    console.log(this.rowData);
    for (let i in this.rowData) {
      for (let j = 0; j < this.formList.length; j++) {
        if (this.formList[j].fieldName == i) {
          this.formList[j].initialValue = this.rowData[i];
        }
      }
    }
  },
  methods: {
    createColumns() {
      return [
        {
          type: 'INPUT',
          label: '仓库',
          fieldName: 'storageName',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '库位',
          fieldName: 'storagePositionCode',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'partNo',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'partName',
          disabled: true
        },
        // {
        //     type: 'INPUT',
        //     label: '当前库存',
        //     fieldName: 'currentStock',
        //     disabled:true
        // },
        {
          type: 'INPUT',
          label: '实际库存',
          fieldName: 'realStock',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '清点数量',
          fieldName: 'checkQuantity',
          change: p => this.changeData(p)
        },
        {
          type: 'INPUT',
          label: '差异数量',
          fieldName: 'profitLossQuantity',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '成本单价',
          fieldName: 'costPrice',
          disabled: true
        }
      ];
    },
    // async
    formChangeHandle(val) {
      console.log(val);
      // let res = await tableEdit(val);
      // console.log(res);
      this.$emit('getDataEditAdd', false, val);
    },
    changeData(row) {
      console.log(row);
      let regex = /^([0-9 ]+)(\.[0-9 ]+)?$/;
      this.flag2 = !regex.test(row);
      let real = this.rowData.realStock;
      if (!this.flag2) {
        for (let i = 0; i < this.formList.length; i++) {
          if (this.formList[i].fieldName == 'profitLossQuantity') {
            this.formList[i].initialValue = row - real;
          }
        }
      } else {
        this.errorFunction();
        return;
      }
    },
    errorFunction() {
      this.$notify.error({
        title: '错误',
        message: '填写错误，此处必须是数字'
      });
    }
  }
};
</script>
<style></style>
